package com.chinemit.cui.utils.popup
{
	 
	
	
	import com.chinemit.cui.utils.ContainerUtil;
	import com.chinemit.cui.utils.SystemUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class PopUpManager
	{
		public static var container:Sprite ;
		
		/**
		 * 添加一个弹出窗体到最q
		 * @param mc
		 * @param parent
		 * @param modal
		 * 
		 */		
		public static function addPopUpToFront( mc:DisplayObject , modal:Boolean=true ):void 
		{
			if( !container ){
				SystemUtil.debug("PopupContainer还未初始化");
				return ;
			}
			if(modal){ 
				var popMask:PopupMask = new PopupMask();
				container.addChild(popMask);
			}
			
			mc.x = (container.width-mc.width)*0.5 ;
			mc.y = (container.height-mc.height)*0.5;
			container.addChild(mc);
		}
		
		/**
		 * 添加某个弹出窗口到最后
		 * @param mc
		 * @param parent
		 * @param modal
		 * 
		 */		
		public static function addPopUpToBehind( mc:DisplayObject , modal:Boolean=true ):void 
		{
			if( !container )
			{
				SystemUtil.debug("PopupContainer还未初始化");
				return ;
			}
			container.addChildAt(mc,0);
			if(modal)
			{
				var popMask:PopupMask = new PopupMask();
				container.addChildAt(popMask,0);
				mc.x = (container.width-mc.width)*0.5 ;
				mc.y = (container.height-mc.height)*0.5;
			}
		}
		
		/**
		 * 移除某个弹出窗口 
		 * @param mc
		 * 
		 */		
		public static function removePopup( mc:DisplayObject ):void 
		{
			if( !container )
			{	
				SystemUtil.debug("PopupContainer还未初始化");
				return ;
			}
			if(mc.parent){
				var index:int = mc.parent.getChildIndex( mc );
				if(index-1>=0 && mc.parent.getChildAt(index-1) is PopupMask)
				{
					mc.parent.removeChildAt( index-1);
				}
				mc.parent.removeChild( mc );
			}
		}
		
		/**
		 * 移除所有弹出窗口 
		 * 
		 */		
		public static  function removeAllPopup():void
		{
			if( !container )
			{
				SystemUtil.debug("PopupContainer还未初始化");
				return ;
			}
			ContainerUtil.removeChildren( container );
		}
	}
}